<template>
<div class="app-container">
  <div style="width: 600px">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <!-- <el-form-item label="标题" prop="title">
        <el-input size="small" v-model="form.title" placeholder="请输入标题" />
      </el-form-item> -->
      <el-form-item label="轮播图" required>
        <div style="color:#999">
          建议尺寸600*230px，JPG、PNG格式， 图片小于5M。
        </div>
        <el-upload
          :class="imgFilesList.length?'hiddenUpload':''"
          action="https://service.bairicun.com/file/upload"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :file-list="imgFilesList"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="跳转类型" prop="title">
        <!-- <el-input size="small" v-model="form.link" placeholder="请输入跳转地址" /> -->
        <el-radio-group v-model="form.title">
          <el-radio label="1">打卡活动</el-radio>
          <el-radio label="2">售卖商品</el-radio>
          <el-radio label="3">跑友故事</el-radio>
          <el-radio label="4">三方链接</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="链接地址" prop="link">
        <el-input v-if="form.title == '1'" size="small" v-model="form.link" placeholder="请输入活动ID" />
        <el-input v-if="form.title == '2'" size="small" v-model="form.link" placeholder="请输入商品ID" />
        <el-input v-if="form.title == '3'" size="small" v-model="form.link" placeholder="请输入故事ID" />
        <el-input v-if="form.title == '4'" size="small" v-model="form.link" placeholder="请输入链接地址" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit('form')">保存</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import {
  saveApi,
  getDetailApi
} from '@/api/banner'
export default {
  data() {
    return {
      form: {
        title: '',
        link: '',
      },
      rules: {
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }, ],
        link: [{
          required: true,
          message: '请输入链接地址',
          trigger: 'blur'
        }, ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadStatus: false,
      uploadUrl: '',
      uploadImgId: '',
      imgFilesList: []
    }
  },
  mounted() {
    getDetailApi(this.$route.query.editId).then((res) => {
      this.form.title = res.data.title
      this.form.link = res.data.url
      this.imgFilesList = [
        {
          url: res.data.picUrl,
          id: res.data.picId,
        }
      ]
      this.uploadImgId = res.data.picId
    })
  },
  methods: {
    handleRemove(file, fileList) {
      this.uploadUrl = ''
      this.uploadImgId = ''
      this.imgFilesList = []
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(e) {
      if(e.code == 200) {
        this.uploadStatus = true
        this.uploadUrl = e.data.url
        this.uploadImgId = e.data.id
        this.imgFilesList = [
          {
            url: e.data.url,
            id: e.data.id,
          }
        ]
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.imgFilesList.length === 0) {
            this.$message.error('请上传缩略图！');
          } else {
            saveApi({
              id: this.$route.query.editId,
              title: this.form.title,
              picId: this.uploadImgId,
              url: this.form.link,
            }).then((res) => {
              this.$message.success('操作成功！');
              setTimeout(() => {
                this.$router.back(-1)
              }, 1000);
            })
          }
        } else {
          return false;
        }
      });
    },
    onCancel() {
      this.$router.back(-1)
    }
  }
}
</script>
<style>
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>
<style scoped>
.line {
  text-align: center;
}
</style>
